package product

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"strconv"
	"vue-admin-server/global"
	"vue-admin-server/model/common/response"
	response2 "vue-admin-server/model/product/response"
)

type ChipProductApi struct{}

func (e *ChipProductApi) GetChipsByPage(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("limit", "20"))
	params := make(map[string]string, 0)
	supplier := c.Query("supplier")
	if len(supplier) != 0 {
		params["supplier"] = supplier
	}
	chipNo := c.Query("chip_no")
	if len(chipNo) != 0 {
		params["chip_no"] = chipNo
	}
	raycusNo := c.Query("raycus_no")
	if len(raycusNo) != 0 {
		params["raycus_no"] = raycusNo
	}
	emitterWidth := c.Query("emitter_width")
	if len(emitterWidth) != 0 {
		params["emitter_width"] = emitterWidth
	}
	moduleType := c.Query("module_type")
	if len(moduleType) != 0 {
		params["module_type"] = moduleType
	}
	meanTimeBetweenFailure := c.Query("mean_time_between_failure")
	if len(meanTimeBetweenFailure) != 0 {
		params["mean_time_between_failure"] = meanTimeBetweenFailure
	}
	beamDivergenceParallel := c.Query("beam_divergence_parallel")
	if len(beamDivergenceParallel) != 0 {
		params["beam_divergence_parallel"] = beamDivergenceParallel
	}
	standardPulseCenterWavelength := c.Query("standard_pulse_center_wavelength")
	if len(standardPulseCenterWavelength) != 0 {
		params["standard_pulse_center_wavelength"] = standardPulseCenterWavelength
	}
	incomingTime := c.Query("incoming_time")
	if len(incomingTime) != 0 {
		params["incoming_time"] = incomingTime
	}

	var p = response2.ChipList{
		Page:        page,
		Limit:       pageSize,
		OrderField:  c.DefaultQuery("orderField", "id"),
		OrderType:   c.DefaultQuery("orderType", "desc"),
		QueryParams: params,
	}
	err := productService.GetChips(&p)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(p, "获取成功", c)
	}
}

func (e *ChipProductApi) GetChipSpec(c *gin.Context) {
	chipIdStr := c.Query("chipId")
	if len(chipIdStr) == 0 {
		response.FailWithMessage("chipId 不能为空", c)
		return
	}

	spec, err := productService.GetChipSpec(chipIdStr)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(*spec, "获取成功", c)
	}
}
